var express   = require('express');
var config    = require('./config');
var routes    = require('./utils/routes');
var utils     = require('./utils/function');
var logger    = require('morgan');
var fs        = require('fs');
var errorLog  = fs.createWriteStream('./logs/error_' +'.log', {flags: 'a'});

var app      = express();
// 调试控制台 访问明细
app.use(logger('dev'));
/*指定路由控制*/
routes(app);

/* 抛出异常处理 */
app.use(function (err, req, res, next) {
	var meta = '[' + new Date() + '] ' + req.url + '\n';
	errorLog.write(meta + err.stack + '\n');
	var ret = config.noreq;
		ret['errmsg'] = err.message || ret['errmsg'];
	res.status(404).send(ret);
});

/*404 处理*/
app.use(function (req, res, next) {
	throw new Error();
});

/*启动服务器*/
var server = app.listen(config.port, function () {
	console.log('App start listening at port %s', config.port);
});